<?php
namespace Admin\Controller;
use Think\Controller;
// 继承公共的控制器
class DrawerController extends CommonController {
    private $user_id;

   public function _initialize(){
       parent::_initialize();

   }

    public function __construct(){
        parent::__construct();
        $this->user_id = is_login();
    }

   public function index()
   {
   	$type = M('drawer');
   	$data = $type->where(array('uid' => $this->user_id))->order('add_time')->select();
   	
   	$this->assign("data", $data);
   	$this->display();
   }


   public function add()
   {
   	$this->display();
   }

   //启用相关栏目，如果启用底层栏目，也需要启用其直接的父级目录(如果被禁用的话)
   public function active_doit()
   {
      $type   = M('category');
      $cateId = I('get.cateid'); //获取cateId
      $data['id'] = $cateId;
      $data['status']   = 1;
      if($type->create($data)){
         $res = $type->save();
         if($res){
            $pid = $type->where('id='.$cateId)->getField('pid');
            $pstatus = $type->where('id='.$pid)->getField('status');
            if(!$pstatus){ //判断直接父级是否被禁用，如果禁用，则启用
               $data2['status'] = 1;
               $res2 = $type->where('id='.$pid)->save($data2);

               $ppid = $type->where('id='.$pid)->getField('pid');
               $ppstatus = $type->where('id='.$ppid)->getField('status');
               if(!$ppstatus){   //判断第二层父级是否被禁用，如果禁用，则启用
                  $data3['status'] = 1;
                  $res3 = $type->where('id='.$ppid)->save($data3);
               }
            }

            $this->success('启用成功',U('Cate/index'));
         }else{
            $this->error('启用失败',U('Cate/index'));
         }
      }
   }

   /**
    * 添加分类
    */
   public function add_doit(){
   		//获得传递过来的分类名称
   		$type = M('drawer');
   		
   		$data['title'] = I('title', '');
   		$data['intro'] = I('intro', '');
   		//如果不给父类id号的话，默认是0
   		$data['uid']  = $this->user_id;
        $data['add_time'] = date('Y-m-d H:i:s');

   		$drawer_id = $type->add($data);
       if (!$drawer_id) $this->error('添加失败', U('Drawer/add'));
       $this->success('添加成功', U('Drawer/index'));
   }


   //删除对应的分类
   //删除当前分类的时候，其子分类也需要删除(将它的status值设为0)
   function delete_doit(){
   	$type = M('category');
      $cateId = I('cateid');

      $data['id'] = $cateId;
      $data['status'] = 0;
   	$res = $type->save($data);
   	if($res){  //判断当前分类是否有子类，如果有的话就禁用其子类...
         $child = $type->where('pid='.$cateId)->getField('id',true);
         if($child){
            $child = array_values($child);
            $data2['id'] = array('in',$child);
            $data2['status'] = 1;
            $map['status'] = 0;
            $res = $type->where($data2)->save($map);

            $sonmap['pid'] = array('in',$child);
            $son = $type->where($sonmap)->getField('id',true);
            if($son){
               $son = array_values($son);
               $data3['id'] = array('in',$son);
               $data3['status'] = 1;
               $sondata['status'] = 0;
               $res = $type->where($data3)->save($sondata);
               // echo $type->getLastSql();
               // return;
            }
         }
   		$this->success('禁用成功',U('Cate/index'));
   	}else{
   		$this->error('禁用失败',U('Cate/index'));
   	}
   }


    /**
     * 修改相应分类名称
     */
   public function update()
   {
    $type = M('drawer');
    $id   = I('id');
    $val  = $type->find($id);

   $this->assign('val',$val);
   $this->display('update');
   }


   //修改分类
   function update_doit(){
   	$type = M('drawer');
   	$data = $type->create();
   	if($data){
   		$res = $type->save($data);
   		if(false !==$res){
   			$this->success('更新成功',U('Drawer/index'));
   		}else{
   			$this->error('更新失败',U('Drawer/index'));
   		}
   	}
   }


}